package hittegods.gui;

import hittegods.classes.*;
import java.awt.Point;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import javax.swing.*;

public class resultElementPnl extends JPanel {

    private Item[] data;
    private JTable table;
    private JScrollPane scrollPane;
    private int[] counts;
    private boolean desc;

    public resultElementPnl(Item[] data) {
        this.data = data;
        desc = true;
        counts = new int[Search.HEADERS.length];
        for (int i = 0; i < counts.length; i++) {
            counts[i] = 0;
        }
        renderTable();
    }

    private void renderTable() {
        MouseListener ml = new MouseListener() {

            @Override
            public void mouseClicked(MouseEvent e) {
            }

            @Override
            public void mousePressed(MouseEvent e) {
                try {
                    int col = table.getTableHeader().columnAtPoint(new Point(e.getX(), e.getY()));
                    counts[col]++;
                    if ((counts[col] % 2) == 0) {
                        desc = false;
                    }
                    Search.sort(data, col, desc);
                    removeAll();
                    repaint();
                    renderTable();

                } catch (Exception ex) {
                }
            }

            @Override
            public void mouseReleased(MouseEvent e) {
            }

            @Override
            public void mouseEntered(MouseEvent e) {
            }

            @Override
            public void mouseExited(MouseEvent e) {
            }
        };
        table = new JTable(Search.parseItemArray(data), Search.HEADERS);
        table.getTableHeader().addMouseListener(ml);
        scrollPane = new JScrollPane(table);
        scrollPane.setBounds(50, 25, 300, 600);
        add(scrollPane);
        repaint();
    }
}
